package com.study.user.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.study.user.entity.DepartmentEntity;
import com.study.user.service.DepartmentService;
import com.study.common.utils.PageUtils;
import com.study.common.utils.R;



/**
 * 
 *
 * @author zhouying
 * @email 395095535@qq.com
 * @date 2022-04-15 16:11:39
 */
@RestController
@RequestMapping("user/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = departmentService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 列表 树
     */
    @RequestMapping("/list/tree")
    public R listTree(){
        List<DepartmentEntity> tree = departmentService.listTree();

        return R.ok().put("page", tree);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		DepartmentEntity department = departmentService.getById(id);

        return R.ok().put("department", department);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody DepartmentEntity department){
		departmentService.save(department);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody DepartmentEntity department){
		departmentService.updateById(department);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		departmentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 删除 tree
     */
    @RequestMapping("/delete/tree")
    public R deleteTree(@RequestParam Integer id){
        Set<Integer> ids=departmentService.deleteTree(id);

        return R.ok().put("ids",ids);
    }

}
